package com.cheer.ba.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.presenter.impl.CloudBookPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.dialog.DialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述 ：申请发票
 * 作者 ：hq
 * 时间 ：2017/5/24 14:14
 */
public class AskInvoiceAty extends BaseCommAty {

    @BindView(R.id.id_invoice_order_num_txt)
    TextView mTxtOrderNum;
    private String mOrderNum;
    @BindView(R.id.id_invoice_money_txt)
    TextView mTxtMoney;
    private String mMoney;
    @BindView(R.id.id_person_check)
    CheckBox mCheckPerson;
    @BindView(R.id.id_compeny_check)
    CheckBox mCheckCom;
    private String mInvoiceType;
    @BindView(R.id.id_invoice_head_edit)
    EditText mEditHead;
    private String mHead;
    @BindView(R.id.id_invoice_num_txt)
    EditText mEditInvoiceNum;
    private String mNum;
    @BindView(R.id.id_bank_name_linear)
    LinearLayout mLinearBankName;
    @BindView(R.id.id_bank_name_txt)
    EditText mEditBankName;
    private String mBankName;
    @BindView(R.id.id_bank_num_linear)
    LinearLayout mLinearBankNum;
    @BindView(R.id.id_bank_num_txt)
    EditText mEditBankNum;
    private String mBankNum;
    @BindView(R.id.id_invoice_name_edit)
    EditText mEditName;
    private String mName;
    @BindView(R.id.id_invoice_phone_txt)
    EditText mEditPhone;
    private String mPhone;
    @BindView(R.id.id_invoice_area_txt)
    TextView mTxtArea;
    private String mArea;
    @BindView(R.id.id_invoice_address_txt)
    EditText mEditAddress;
    private String mAddress;

    private MyOrderModel model;


    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_add_invoice, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initPresenter() {
        presenter = new CloudBookPresenter();
    }

    @Override
    public void initView() {
        model = (MyOrderModel) getIntent().getSerializableExtra(IntentKey.General.KEY_MODEL);
        if (null != model) {
            mTxtOrderNum.setText(model.getOrderId());
            mTxtMoney.setText(Currency.returnDollar(model.getCostTrue()));
        }
        setTitleText(getString(R.string.ask_invoice));
        setTitleRight(getString(R.string.sure_btn), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发票申请
                if (checkData()) {
                    if (null != model) {
                        showInvoiceDialog();
                    } else {
                        SVProgressHUD.showInfoWithStatus(aty, "数据错误");
                    }
                }
            }
        });
    }

    private Dialog dialog;

    /**
     * 弹出框提醒
     */
    public void showInvoiceDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        View view = getLayoutInflater().inflate(R.layout.login_remind, null, false);
        TextView mTvTitle = (TextView) view.findViewById(R.id.id_remind_login_txt);
        mTvTitle.setText("寄票方式为到付");
        mTvTitle.setSingleLine(false);
        TextView mTxtCancel = (TextView) view.findViewById(R.id.id_login_cancel_txt);
        mTxtCancel.setText("取消");
        mTxtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView mTxtConfirm = (TextView) view.findViewById(R.id.id_login_confirm_txt);
        mTxtConfirm.setText("同意");
        mTxtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ((CloudBookPresenter) presenter).askMyInvoice(mInvoiceType, model.getCostTrue(), model.getOrderId(), mNum, mHead, mBankName, mBankNum, mName, mPhone, mArea + "|" + mAddress);
            }
        });

        dialog = DialogUtils.selfDialog(this, view, true);
        dialog.show();
    }

    /**
     * 检测信息
     */
    private boolean checkData() {
        mHead = mEditHead.getText().toString().trim();
        mNum = mEditInvoiceNum.getText().toString().trim();
        mName = mEditName.getText().toString().trim();
        mPhone = mEditPhone.getText().toString().trim();
        mAddress = mEditAddress.getText().toString().trim();
        mBankName = mEditBankName.getText().toString().trim();
        mBankNum = mEditBankNum.getText().toString().trim();

        if (StringUtils.isEmpty(mInvoiceType)) {
            SVProgressHUD.showInfoWithStatus(aty, "请选择发票类型");
        } else {
            if (StringUtils.isEmpty(mHead)) {
                SVProgressHUD.showInfoWithStatus(aty, "请填写发票抬头");
            } else {
                if (!TextUtils.equals("1", mInvoiceType)) {
                    if (StringUtils.isEmpty(mNum)) {
                        SVProgressHUD.showInfoWithStatus(aty, "请填写税号");
                    } else {
                        if (StringUtils.isEmpty(mBankName)) {
                            SVProgressHUD.showInfoWithStatus(aty, "请填写开户行名称");
                        } else {
                            if (StringUtils.isEmpty(mBankNum)) {
                                SVProgressHUD.showInfoWithStatus(aty, "请填写开户行银行账号");
                            } else {
                                return checkOther();
                            }
                        }
                    }
                } else {
                    return checkOther();
                }
            }
        }
        return false;
    }

    public boolean checkOther() {
        if (StringUtils.isEmpty(mName)) {
            SVProgressHUD.showInfoWithStatus(aty, "请填写收件人姓名");
        } else {
            if (StringUtils.isEmpty(mPhone) || !StringUtils.isMobile(mPhone)) {
                SVProgressHUD.showInfoWithStatus(aty, "请填写收件人联系方式");
            } else {
                if (StringUtils.isEmpty(mArea)) {
                    SVProgressHUD.showInfoWithStatus(aty, "请选择收件地区");
                } else {
                    if (StringUtils.isEmpty(mAddress)) {
                        SVProgressHUD.showInfoWithStatus(aty, "请输入收件详细地址");
                    } else {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.DEFAULT_SUCCESS, tag)) {
                String msg = (String) t;
                SVProgressHUD.showSuccessWithStatus(aty, msg);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setResult(IntentCode.Invoice.INVOICE_RESULT_CODE);
                        finish();
                    }
                }, 2000);
            }
        }
    }

    @OnClick({R.id.id_person_check, R.id.id_compeny_check, R.id.id_invoice_area_txt})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //普通发票
            case R.id.id_person_check:
                mCheckPerson.setChecked(true);
                mCheckCom.setChecked(false);
                mInvoiceType = "1";
                mLinearBankName.setVisibility(View.GONE);
                mLinearBankNum.setVisibility(View.GONE);
                mBankName = "";
                mBankNum = "";
                break;
            //增值税发票
            case R.id.id_compeny_check:
                mCheckPerson.setChecked(false);
                mCheckCom.setChecked(true);
                mInvoiceType = "2";
                mLinearBankName.setVisibility(View.VISIBLE);
                mLinearBankNum.setVisibility(View.VISIBLE);
                break;
            case R.id.id_invoice_area_txt:
                Intent email = new Intent(aty, ProvinceAreaAty.class);
                startActivityForResult(email, IntentCode.Area.PROVINCE_REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Area.PROVINCE_REQUEST_CODE) {
            if (resultCode == IntentCode.Area.PROVINCE_RESULT_CODE) {
                if (null != data) {
                    mArea = data.getStringExtra(IntentKey.General.KEY_DATA);
                    mTxtArea.setText(mArea);
                    Session.setUser_area(mArea);
                    String[] s = StringUtils.strSplit(mArea);
                    mArea = s[0] + "|" + s[1] + "|" + s[2];
                }
            }
        }
    }
}
